﻿using Ardalis.GuardClauses;
using DDD.Core.Const;
using DDD.Core.Exceptions.Bussines;
using DDD.Core.Exceptions.Bussines.Regex;
using DDD.Core.Exceptions.System;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

namespace DDD.Core.Vaildate
{
    public static class GuardExtension
    {
        public static string ValueIsNotNullOrEmpty(this IGuardClause guardClause, string input, [CallerArgumentExpression("input")] string? configurationProp = null)
        {
            if (string.IsNullOrEmpty(input))
                throw new ValueIsNullOrEmptyException(configurationProp);

            return input;
        }

        public static string ConfigurationValueNotIsNullOrEmpty(this IGuardClause guardClause, string input, [CallerArgumentExpression("input")] string? configurationProp = null)
        {
            if (string.IsNullOrEmpty(input))
                throw new ConfigurationIsNullOrEmptyException(configurationProp);

            return input;
        }

        /// <summary>
        /// 验证邮箱规范
        /// </summary>
        /// <param name="guardClause"></param>
        /// <param name="input"></param>
        /// <param name="configurationProp"></param>
        /// <returns></returns>
        /// <exception cref="EmailException"></exception>
        public static string EmailFormatIsRight(this IGuardClause guardClause, string input)
        {
            return RegexFormatIsRight(input, RegexConst.EMAIL, new EmailException(input));
        }

        /// <summary>
        /// 验证手机规范
        /// </summary>
        /// <param name="guardClause"></param>
        /// <param name="input"></param>
        /// <param name="configurationProp"></param>
        /// <returns></returns>
        /// <exception cref="EmailException"></exception>
        public static string PhoneFormatIsRight(this IGuardClause guardClause, string input)
        {
            return RegexFormatIsRight(input, RegexConst.PHONE, new PhoneException(input));
        }

        /// <summary>
        /// 自定义正则规范
        /// </summary>
        /// <param name="guardClause"></param>
        /// <param name="input"></param>
        /// <param name="regex"></param>
        /// <param name="configurationProp"></param>
        /// <returns></returns>
        public static string CustomRegexFormatIsRight(this IGuardClause guardClause, string input, string regex)
        {
            return RegexFormatIsRight(input, regex, new RegexException(input));
        }

        /// <summary>
        /// 验证正则规范
        /// </summary>
        /// <param name="guardClause"></param>
        /// <param name="input"></param>
        /// <param name="regex"></param>
        /// <param name="regexException"></param>
        /// <returns></returns>
        private static string RegexFormatIsRight(string input, string regex, RegexException regexException)
        {
            if (!string.IsNullOrEmpty(input))
            {
                if (Regex.IsMatch(input, regex))
                    return input;

                throw regexException;
            }

            return input;
        }

    }
}
